package org.stvd.controller.quartz;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.aspectj.lang.enums.OperatorType;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.quartz.QuartzJob;
import org.stvd.service.quartz.QuartzJobService;
import org.stvd.service.quartz.impl.QuartzHandler;

/**
 * @title QuartzJobController
 * @description 定时任务控制类
 * @auther houzx
 * @date 2022年03月10日
 */
@Controller
@RequestMapping("quartz/job")
public class QuartzJobController extends BaseController{

    @Autowired
    private QuartzJobService quartzJobService;
    @Autowired
    private QuartzHandler quartzHandler;
    
    private String tmpURL = "/quartz/job/";

    /**
     * 获取定时任务列表
     * @param quartzJob 任务信息
     * @return 任务列表
     */
    @RequestMapping(value = "manage", method = {RequestMethod.GET, RequestMethod.POST})
    public String listLogs(@ModelAttribute QuartzJob quartzJob) {
        QueryResult<QuartzJob> queryResult = quartzJobService.queryQuartzJobResult(
            (pageNo - 1) * pageSize, pageSize, quartzJob);
        map.put("queryResult", queryResult);
        map.put("isInStand", quartzHandler.isInStandbyMode());
        return tmpURL + "JobManage.html";
    }
    
    /**
     * 新增任务信息
     * @param quartzJob
     * @return
     */
    @GetMapping(value = "add")
    public String onQuartzJobAdd(@ModelAttribute QuartzJob quartzJob) {
        return tmpURL + "JobAdd.html";
    }

    @PostMapping(value = "add")
    @ResponseBody
    @Log(title = "新增定时任务", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> insertQuartzJob(@Valid @ModelAttribute QuartzJob quartzJob, BindingResult bindingResult) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        if (bindingResult.hasErrors()) {
            result.setError(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return result;
        }
        result.transferResult(quartzJobService.insertQuartzJob(quartzJob));
        return result;
    }

    /**
     * 修改任务信息
     * @param quartzJob
     * @return
     */
    @GetMapping(value = "edit/{jobId}")
    public String onQuartzJobEdit(@PathVariable Long jobId) {
        QuartzJob quartzJob = quartzJobService.findByPk(QuartzJob.class, jobId);
        map.put("quartzJob", quartzJob);
        return tmpURL + "JobEdit.html";
    }
    
    @PostMapping(value = "edit")
    @ResponseBody
    @Log(title = "更新定时任务", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> updateQuartzJob(@Valid @ModelAttribute QuartzJob quartzJob, BindingResult bindingResult) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        if (bindingResult.hasErrors()) {
            result.setError(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return result;
        }
        result.transferResult(quartzJobService.updateQuartzJob(quartzJob));
        return result;
    }
    
    /**
     * 获取任务详情
     * @param jobId 任务ID
     * @return 任务详情
     */
    @GetMapping(value = "detail/{jobId}")
    public String detailQuartzJob(@PathVariable Long jobId) {
        QuartzJob quartzJob =  quartzJobService.findByPk(QuartzJob.class, jobId);
        map.put("quartzJob", quartzJob);
        return tmpURL + "JobDetail.html";
    }

    /**
     * 删除任务信息
     * @param jobId 任务ID
     * @return 任务删除结果
     */
    @PostMapping(value = "delete/{jobId}")
    @ResponseBody
    @Log(title = "删除定时任务", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> deleteQuartzJob(@PathVariable Long jobId) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        result.transferResult(quartzJobService.deleteQuartzJob(jobId));
        return result;
    }

    /**
     * 启动定时任务
     * @param jobId 任务ID
     * @return 启动结果
     */
    @PostMapping(value = "start/{jobId}")
    @ResponseBody
    @Log(title = "启动定时任务", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> startQuartzJob(@PathVariable Long jobId) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        result.transferResult(quartzJobService.startQuartzJob(jobId));
        return result;
    }

    /**
     * 暂停定时任务
     * @param jobId 任务ID
     * @return 暂停结果
     */
    @PostMapping(value = "pause/{jobId}")
    @ResponseBody
    @Log(title = "暂停定时任务", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> pauseQuartzJob(@PathVariable Long jobId) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        result.transferResult(quartzJobService.pauseQuartzJob(jobId));
        return result;
    }

    /**
     * 立即执行定时任务
     * @param jobId 任务ID
     * @return 执行结果
     */
    @PostMapping(value = "execute/{jobId}")
    @ResponseBody
    @Log(title = "立即执行定时任务", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> executeQuartzJob(@PathVariable Long jobId) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        result.transferResult(quartzJobService.executeQuartzJob(jobId));
        return result;
    }

}
